package com.jack.helloworld.activity;

import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.jack.helloworld.R;

import java.util.ArrayList;

public class GestureOverlayViewActivity extends AppCompatActivity implements GestureOverlayView.OnGesturePerformedListener {

    GestureOverlayView gestureOverlayView;
    GestureLibrary gestureLibrary;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gesture_overlay_view);

        gestureOverlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView);

        gestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gesture);
        gestureLibrary.load();
        gestureOverlayView.addOnGesturePerformedListener(this);

    }

    @Override
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
        ArrayList<Prediction> recognize = gestureLibrary.recognize(gesture);
        Prediction prediction = recognize.get(0);

        Log.e("jack", "recognize:" + recognize);
        Log.e("jack", "prediction:" + recognize.get(0) + " ,score:" + recognize.get(0).score);
        Log.e("jack", "prediction:" + recognize.get(1) + " ,score:" + recognize.get(1).score);
        Log.e("jack", "prediction:" + recognize.get(2) + " ,score:" + recognize.get(2).score);
        Log.e("jack", "prediction:" + recognize.get(3) + " ,score:" + recognize.get(3).score);

        if (prediction.score > 5.0) {

            if (prediction.name.equals("toRight")) {

            } else if (prediction.name.equals("toLeft")) {

            } else if (prediction.name.equals("open")) {

            } else if (prediction.name.equals("close")) {

            }
            Toast.makeText(this, "找到手势:" + prediction.name, Toast.LENGTH_SHORT).show();


        } else {
            Toast.makeText(this, R.string.error_gesture, Toast.LENGTH_SHORT).show();
        }

    }
}
